// store/index.js

import { createStore } from 'vuex';
import Cookies from 'js-cookie';
import cache from "@/plugins/cache";
const store = createStore({
  state() {
    return {
      addEvents:[],
      events: [],
      planQurry:[],
      guidEdit:true,
    };
  },
  mutations: {
    setGuidEdit(state){
      state.guidEdit = true
    },
    setFalseGuidEdit(state){
      state.guidEdit = false
    },
    setPlanQurry(state,planQurry){
        state.planQurry = planQurry
    },
    saveAddEventsCooike(state){
      // let events = [];
      // if(cache.session.getJSON('events') != null &&  cache.session.getJSON('events').length>0){
      //   events = cache.session.getJSON('events');
      // }

      // for (let index = 0; index < state.events.length; index++) {
      //   const element = state.events[index];
      //   events.push(element)
      // }
      cache.session.setJSON('events',state.events);
    },
    saveEventsAdded(state,events){
      state.events = events
     
    },
    addEvents(state, item) {
      state.events.push(item) ;
    },
    delEvents(state, event) {
      // 使用 filter 方法来保留所有不等于指定 id 的元素
      const newArray = state.events.filter(item => item.id !== event.id);
      state.events = newArray;
      // Cookies.set('events',JSON.stringify(state.events));
    },
    clearEvents(state){
        state.events = [] ;
    }
  },
  actions: {
    someGlobalMethod({ commit }) {
      console.log('这是通过 Vuex 管理的全局方法');
    },
    getPlanQurry(){

    },
  }
});

export default store;